package com.nimbusung;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;
import com.nimbusung.domain.Song;

public class VideosActivity extends Activity {

	private List<Song> songs =  new ArrayList<Song>(); 
	private VideoView webView;
	private TextView artist; 
	private TextView title;
	private String yLink;
	private int picture;
	private ImageView imageCover;
	private Context context;
	
	
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.videos_layout);
		context = this;
		setUpSongs();

		LinearLayout l = (LinearLayout) findViewById(R.id.myVideoLayout);
		LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		webView = (VideoView)findViewById(R.id.webViewVideo);
		MediaController mediaC = new MediaController(this);
		webView.setMediaController(mediaC);
		webView.isSoundEffectsEnabled();
		
		webView.setBackgroundColor(Color.WHITE);
		
		webView.requestFocus();
		//webView.setVideoURI(Uri.parse("rtsp://v8.cache8.c.youtube.com/CjYLENy73wIaLQkAcXXxyczNyBMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYKy56eHU3JicTAw=/0/0/0/video.3gp"));
		//webView.start();

		for(final Song aSong : songs){

			View customView = linflater.inflate(R.layout.video_song_row, null);

			artist = (TextView) customView.findViewById(R.id.text_artist);
			title = (TextView) customView.findViewById(R.id.text_song);
			imageCover = (ImageView) customView.findViewById(R.id.imageCover);

			artist.setText(aSong.getArtist());
			title.setText(aSong.getTitle());
			imageCover.setImageResource(aSong.getDrawPicture());
			yLink = aSong.getYoutubelink();
			picture = aSong.getDrawPicture();
			

			customView.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					webView.setBackgroundResource(aSong.getDrawPicture());
					
					//webView.setVideoURI(Uri.parse(aSong.getYoutubelink()));
					//webView.start();
					String videolink= aSong.getYoutubelink();
					Uri uri = Uri.parse(videolink);
			        String vid = uri.getQueryParameter("v");
			        String sig = uri.getQueryParameter("sig");
			        
			        
			        Intent lVideoIntent = new Intent(null,Uri.parse("ytv://"+vid), context, OpenYouTubePlayerActivity.class);
			        startActivity(lVideoIntent);
				}
			});

			//Add view
			l.addView(customView);
		}
	}


	private void setUpSongs(){
		songs.add(new Song("http://www.youtube.com/watch?v=ijwtA3pzyLs", "Ära Till Ditt Namn", "Mattias Martinsson",R.drawable.vp1));
		songs.add(new Song("http://www.youtube.com/watch?v=7hHZlXe_2Hk", "Så stor är vår Gud", "Robert Eriksson", R.drawable.vp2));
		//songs.add(new Song("rtsp://v7.cache2.c.youtube.com/CjYLENy73wIaLQnyLXqO-y0nNhMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYKy56eHU3JicTAw=/0/0/0/video.3gp", "Ropa till Gud","Bengt Johansson", R.drawable.vp3));
		songs.add(new Song("http://www.youtube.com/watch?v=Nict-456LfI", "Ropa till Gud","Bengt Johansson", R.drawable.vp3));
	}

}

